﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using EHR.Common;

namespace CommunitySystem
{
    public partial class DiaoDong_DiaoChuBenQuControl1 : UserControl
    {
        public ObservableCollection<BianZhiModel> BianZhiModels = new ObservableCollection<BianZhiModel>();
        private BanLiZhuangTai shiti = new BanLiZhuangTai();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        public ObservableCollection<EmpbasicService.OrgForHRModel> OrgList =
            new ObservableCollection<EmpbasicService.OrgForHRModel>();
        ObservableCollection<EmpbasicService.OrgForHRModel> forumList = new ObservableCollection<EmpbasicService.OrgForHRModel>();
        private DiaoDongService.DiaoDongServiceClient client1 = new DiaoDongService.DiaoDongServiceClient();
        public DiaoDong_DiaoChuBenQuControl1()
        {
            InitializeComponent();
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cb = new ComboBoxItem();
                cb.Content = i;
                cb.Tag = i;
                status.Items.Add(cb);
            }
            shiti.blzt = "全部";
            ygzt.DataContext = shiti;
            ListBoxOfLife.Items.Clear();
            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client_GetOrgForHRModelbyIDsCompleted);
            client1.GetDiaoDongCompleted += new EventHandler<DiaoDongService.GetDiaoDongCompletedEventArgs>(client1_GetDiaoDongCompleted);
            Client.GetDengLuOrgCompleted += new EventHandler<GetDengLuOrgCompletedEventArgs>(Client_GetDengLuOrgCompleted);
            client1.ShenHeDiaoDongCompleted += new EventHandler<DiaoDongService.ShenHeDiaoDongCompletedEventArgs>(client1_ShenHeDiaoDongCompleted);
            client1.CancelShenHeDiaoDongCompleted += new EventHandler<DiaoDongService.CancelShenHeDiaoDongCompletedEventArgs>(client1_CancelShenHeDiaoDongCompleted);
            client1.TuiHuiShenHeDiaoDongCompleted += new EventHandler<DiaoDongService.TuiHuiShenHeDiaoDongCompletedEventArgs>(client1_TuiHuiShenHeDiaoDongCompleted);
            client1.ChaRuRenYuanKuCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client1_ChaRuRenYuanKuCompleted);
            client1.DeleteShenHeDiaoDongCompleted += new EventHandler<DiaoDongService.DeleteShenHeDiaoDongCompletedEventArgs>(client1_DeleteShenHeDiaoDongCompleted);
            client1.CanShenHeDiaoDongCompleted += new EventHandler<DiaoDongService.CanShenHeDiaoDongCompletedEventArgs>(client1_CanShenHeDiaoDongCompleted);
            client1.TongYiDiaoDongCompleted += new EventHandler<DiaoDongService.TongYiDiaoDongCompletedEventArgs>(client1_TongYiDiaoDongCompleted);
            Client.GetBianZhiDetailModelCompleted += new EventHandler<GetBianZhiDetailModelCompletedEventArgs>(Client2_GetBianZhiDetailModelCompleted);
        }

        #region 左右拖动的条
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0.0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }
        #endregion
        public String[] Oldorgids;
        void Client_GetDengLuOrgCompleted(object sender, EmpbasicService.GetDengLuOrgCompletedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Result))
            {
                Oldorgids = e.Result.Split(',');
            }
            Client.GetOrgForHRModelbyIDsAsync("(" + e.Result + ")");
        }
        void client1_GetDiaoDongCompleted(object sender, DiaoDongService.GetDiaoDongCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            if (e.Result != null && e.Result.Count > 0)
            {
                for (int i = 0; i < e.Result.Count; i++)
                {
                    e.Result[i].Sec = i + 1;
                    if (e.Result[i].RZQMSJ == null && Convert.ToDateTime(e.Result[i].RZQMSJ) == Convert.ToDateTime("1900-01-01")) e.Result[i].RZQMSJ = null;
                }
                DGRole.ItemsSource = e.Result;


            }
        }
        #region 获得组织机构树
        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            OrgList = e.Result;
            forumList.Clear();
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            AddTreeNode("全部单位", null);
        }

        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();

                    //StackPanel sp = new StackPanel();
                    CheckBox cb = new CheckBox();
                    cb.Content = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                    //sp.Children.Add(cb);
                    cb.Click += new RoutedEventHandler(cb_Checked);
                    objTreeNode.Header = cb;
                    //objTreeNode.Header = foruminfo.Name;
                    //objTreeNode.DataContext = foruminfo;
                    //objTreeNode.Tag = foruminfo.ID;
                    if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;

                    //此样式将会添加的所有叶子结点上
                    //objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;

                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }
        public int orgid { get; set; }
        public List<string> orgids = new List<string>();
        public string type { get; set; }
        public string SelectOrgName = "";
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
                SelectOrgName = ((EmpbasicService.OrgForHRModel)cb.DataContext).Name;
                //if (orgids != null)
                //{
                //orgid = Convert.ToInt32(((EmpbasicService.OrgForHRModel)cb.DataContext).ID);
                //tbTitle.Text = cb.Content.ToString();
                if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                    orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (string.IsNullOrEmpty(ss) || orgids.Contains("79")) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
                LoadData(ss);
                CheckChildItem(cb.Tag as TreeViewItem);
            }
            else
            {
                //if (orgids != null)
                //{
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (ss.Length > 1) ss = ss.Substring(0, ss.Length - 1);
                LoadData(ss);
                UnCheckChildItem(cb.Tag as TreeViewItem);
            }
        }

        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        #endregion
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            orgid = Convert.ToInt32(((OrgForHRModel)(ListBoxOfLife.SelectedItem)).ID);
            orgids.Clear();
            orgids.Add(orgid.ToString());
            LoadData(orgids[0]);
        }

        void Client_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
        {
            ListBoxOfLife.ItemsSource = e.Result;
            ListBoxOfLife.SelectedIndex = 0;
            orgid = Convert.ToInt32(e.Result[0].ID);
            orgids.Clear();
            orgids.Add(orgid.ToString());
            LoadData(orgids[0]);
        }

        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            if (e.Result != "人事干部")
            {
                if (TreeOfLife.ItemsSource != null) TreeOfLife.ItemsSource = null;
                TreeOfLife.Visibility = System.Windows.Visibility.Visible;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Collapsed;
                Client.GetOrgForHRModelAsync(0);
                tongyi.Visibility = System.Windows.Visibility.Visible;
                CheckData.Visibility = System.Windows.Visibility.Visible;
                CancelCheckData.Visibility = System.Windows.Visibility.Visible;

                LoadData("0");
            }
            else
            {
                TreeOfLife.Visibility = System.Windows.Visibility.Collapsed;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Visible;
                tongyi.Visibility = System.Windows.Visibility.Collapsed;
                CheckData.Visibility = System.Windows.Visibility.Collapsed;
                CancelCheckData.Visibility = System.Windows.Visibility.Collapsed;
                Client.GetDengLuOrgAsync(Session.LoginUser.LoginName);
            }
        }

        public void LoadData(string orgids)
        {
            client1.GetDiaoDongAsync(orgids, 0, "调出本区", ((ComboBoxItem)status.SelectedItem).Content.ToString(), shiti.blzt);
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            if (orgids.Count != 1)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能针对一个单位进行人员调动的新增！");
                return;
            }
            //DiaoDongTypeWindow RMWindow = new DiaoDongTypeWindow(Convert.ToInt32(orgids[0]), 0, true, "非查看",SelectOrgName);
            //RMWindow.Closed += new EventHandler(RMWindow_Closed);
            //RMWindow.Show();

            DiaoDongWindow_DiaoChuBenQu RMWindow = new DiaoDongWindow_DiaoChuBenQu(Convert.ToInt32(orgids[0]), 0, true, "非查看", "调出本区");
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }

        void RMWindow_Closed(object sender, EventArgs e)
        {
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as DiaoDongService.DiaoDongModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                if (seljw.Status == "审核通过" || seljw.Status == "审核不通过")
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员调动不可进行编辑操作！");
                    return;
                }
                DiaoDongWindow_DiaoChuBenQu RMWindow = new DiaoDongWindow_DiaoChuBenQu(seljw.NewOrgID, Convert.ToInt32(seljw.ID), false, "非查看", "");
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的人员调动！");
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as DiaoDongService.DiaoDongModel;

            if (seljw != null)
            {
                if (seljw.Status == "审核通过" || seljw.Status == "审核不通过")
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员调动不可进行删除操作！");
                    return;
                }
                client1.DeleteShenHeDiaoDongAsync(seljw.ID);
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的人员调动！");
        }
        void client1_DeleteShenHeDiaoDongCompleted(object sender, DiaoDongService.DeleteShenHeDiaoDongCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }

        void client1_ChaRuRenYuanKuCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("入库成功！");
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }
        void client1_ShenHeDiaoDongCompleted(object sender, DiaoDongService.ShenHeDiaoDongCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("审核成功！");
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);

        }
        void client1_CancelShenHeDiaoDongCompleted(object sender, DiaoDongService.CancelShenHeDiaoDongCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("取消审核成功！");
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }
        void client1_TuiHuiShenHeDiaoDongCompleted(object sender, DiaoDongService.TuiHuiShenHeDiaoDongCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("退回成功！");
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }
        private DiaoDongService.DiaoDongModel ss = new DiaoDongService.DiaoDongModel();
        private void CheckData_Click(object sender, RoutedEventArgs e)
        {
            ss = this.DGRole.SelectedItem as DiaoDongService.DiaoDongModel;

            if (ss != null)
            {
                if (ss.Status == "审核通过" || ss.Status == "审核不通过" || ss.Status == "人员已入库" || ss.TongYiStatus != "同意")
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员调动不可进行审核通过操作！");
                    return;
                }
                Client.GetBianZhiDetailModelAsync(ss.NewOrgID.ToString(), "");

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要审核通过的人员调动！");
        }
        void Client2_GetBianZhiDetailModelCompleted(object sender, GetBianZhiDetailModelCompletedEventArgs e)
        {
            BianZhiModels = e.Result;
            if (BianZhiModels != null && BianZhiModels.Count > 0)
            {
                if (BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true))
                    {
                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.zhengkeshiyou) ? Convert.ToInt32(p.zhengkeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }

                if (BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
                    {
                        i.FuKeShiJi =
                            (Convert.ToInt32(
                                BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                                    FirstOrDefault().FuKeShiJi) +
                             BianZhiModels.Sum(
                                 p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
                             BianZhiModels.Sum(
                                 p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
                                .ToString();

                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? Convert.ToInt32(i.ZhengKeFeiLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }
                if (ss.DutyLevel == "乡科级正职（主任科员）" && ss.DutyAttribute == "领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == ss.NewOrgID && p.JiGouMingCheng == ss.NewPositionName && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhengKeShiJi) ? p.ZhengKeShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                        return;
                    }
                }
                if (ss.DutyLevel == "乡科级副职（副主任科员）" && ss.DutyAttribute == "领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == ss.NewOrgID && p.JiGouMingCheng == ss.NewPositionName && Convert.ToInt32(!string.IsNullOrEmpty(p.FuKeShiJi) ? p.FuKeShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                        return;
                    }
                }
                if (ss.DutyLevel == "乡科级正职（主任科员）" && ss.DutyAttribute == "非领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == ss.NewOrgID && p.JiGouMingCheng == ss.NewPositionName && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhuRenShiJi) ? p.ZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                        return;
                    }
                }
                if (ss.DutyLevel == "乡科级副职（副主任科员）" && ss.DutyAttribute == "非领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == ss.NewOrgID && p.JiGouMingCheng == ss.NewPositionName && Convert.ToInt32(!string.IsNullOrEmpty(p.FuZhuRenShiJi) ? p.FuZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                        return;
                    }
                }
                if (MessageBox.Show("确认审核通过么？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    //client1.CanShenHeDiaoDongAsync(seljw);
                    client1.ShenHeDiaoDongAsync(ss.ID, "审核通过", ss);
                }
            }
        }
        private void CancelCheckData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as DiaoDongService.DiaoDongModel;

            if (seljw != null)
            {
                if (seljw.Status == "审核通过" || seljw.Status == "审核不通过" || seljw.Status == "人员已入库" || seljw.TongYiStatus != "同意")
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员调动不可进行审核不通过操作！");
                    return;
                }
                if (MessageBox.Show("确认审核不通过么？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    client1.CancelShenHeDiaoDongAsync(seljw.ID, "审核不通过");
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要审核不通过的人员调动！");
        }
        void client1_CanShenHeDiaoDongCompleted(object sender, DiaoDongService.CanShenHeDiaoDongCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员已入库，不可重复操作！");
                return;
            }
            else
            {
                var seljw = this.DGRole.SelectedItem as DiaoDongService.DiaoDongModel;
                if (MessageBox.Show("确认将该人员入库么？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    client1.ChaRuRenYuanKuAsync(seljw);
                }

            }

        }
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as DiaoDongService.DiaoDongModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                //if (seljw.Type == "区内调动")
                //{
                DiaoDongWindow_DiaoChuBenQu RMWindow = new DiaoDongWindow_DiaoChuBenQu(seljw.OrgID, Convert.ToInt32(seljw.ID), false, "查看", "");
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();
                //}
                //else if (seljw.Type == "调任" || seljw.Type == "外区调入" || seljw.Type == "外省市调入")
                //{
                //    DiaoDongWindow RMWindow = new DiaoDongWindow(0, Convert.ToInt32(seljw.ID), false, "查看", "");
                //    RMWindow.Closed += new EventHandler(RMWindow_Closed);
                //    RMWindow.Show();
                //}
                //else if (seljw.Type == "单位内转任")
                //{
                //    DiaoDongWindow_DanWeiNeiZhuanRen RMWindow = new DiaoDongWindow_DanWeiNeiZhuanRen(0, Convert.ToInt32(seljw.ID), false, "查看",SelectOrgName);
                //    RMWindow.Closed += new EventHandler(RMWindow_Closed);
                //    RMWindow.Show();
                //}
                //else
                //{
                //    DiaoDongWindow_DiaoChuBenQu RMWindow = new DiaoDongWindow_DiaoChuBenQu(0, Convert.ToInt32(seljw.ID), false, "查看", "");
                //    RMWindow.Closed += new EventHandler(RMWindow_Closed);
                //    RMWindow.Show();
                //}

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要查看的人员调动！");
        }

        private void TongYi_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as DiaoDongService.DiaoDongModel;

            if (seljw != null)
            {
                if (seljw.Status == "审核通过" || seljw.Status == "人员已入库")
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员调动不可进行同意申请操作！");
                    return;
                }
                if (MessageBox.Show("确认同意申请么？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    client1.TongYiDiaoDongAsync(seljw.ID, "同意");
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要同意申请的人员调动！");
        }
        void client1_TongYiDiaoDongCompleted(object sender, DiaoDongService.TongYiDiaoDongCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("同意申请成功！");
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }
    }
}
